using Yarp.ReverseProxy;
using Microsoft.Extensions.ServiceDiscovery;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

var builder = WebApplication.CreateBuilder(args);

// 添加Aspire服务默认配置
builder.AddServiceDefaults();

// 配置YARP
var reverseProxyBuilder = builder.Services.AddReverseProxy()
    .LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));

reverseProxyBuilder.AddServiceDiscoveryDestinationResolver();

// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();

var app = builder.Build();

// 配置Aspire默认端点
app.MapDefaultEndpoints();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.MapOpenApi();
}

//app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

// 配置YARP中间件
app.MapReverseProxy();

// 配置Swagger
app.UseSwaggerUI(opts =>
{
    opts.SwaggerEndpoint("/openapi/v1.json", "gw api");
    opts.SwaggerEndpoint("/core/swagger/All%20Groups/swagger.json", "core api");
    opts.SwaggerEndpoint("/base/swagger/All%20Groups/swagger.json", "base api");
    opts.RoutePrefix = string.Empty;
});

app.Run();
